/* Info.java
 * 
 * Alfred Olschnoegger
 * 09.06.2008
 */

package presentation.controller;

import java.util.ArrayList;
import java.util.List;

import exceptions.LogWriter;
import application.GameInfoProvider;
import application.ServerInfoProvider;
import application.valueObjects.GameObjectValue;
import application.valueObjects.GameValue;
import application.valueObjects.HunterValue;
import application.parser.Parser;
import application.parser.StringParser;

/**
 * TODO description
 *
 * @date 09.06.2008
 * @author Alfred Olschnoegger
 */

public class Info  extends HttpBaseController {

	@Override
	public void process() {
		StringParser parse = new StringParser();
		
		if(m_Arguments.get("task").equalsIgnoreCase("server")) {
			ServerInfoProvider prov = new ServerInfoProvider();
			prov.getInfo(parse);
			m_Request.setAttribute("responseStr",parse.parse());
			redirect();
		} else if(m_Arguments.get("task").equalsIgnoreCase("game")) {
			ServerInfoProvider prov = new ServerInfoProvider();
			prov.getGames(parse);
			m_Request.setAttribute("responseStr",parse.parse());
			redirect();
		} else if(m_Arguments.get("task").equalsIgnoreCase("status")) {
			GameInfoProvider prov;
			try {
				prov = new GameInfoProvider(Integer.parseInt(m_Arguments.get("game")));
				GameValue g = prov.getGameValue();
				List<HunterValue> hunters = prov.getValidHunterValues();
				List<GameObjectValue> objs = prov.getObjectValues();
				
				ArrayList<ArrayList<FieldBean>> map = new ArrayList<ArrayList<FieldBean>>();
				System.out.println(g.getXMax());
				System.out.println(g.getYMax());
				for(int y = 0; y <= g.getYMax();y++) {
					map.add(new ArrayList<FieldBean>());
					for(int x = 0; x <= g.getXMax();x++)
						map.get(y).add(new FieldBean());
				}
				
				for(GameObjectValue o : objs) {
					if(o.getObjectIdentifier() == o.RABBIT_IDENTIFIER) {
						map.get((o.getY() - g.getYMax())*-1).get(o.getX()).setImage("rabbit.png");
						map.get((o.getY() - g.getYMax())*-1).get(o.getX()).setName("Rabbit");
					}
				}
				
				
				
				for(HunterValue h : hunters) {
					map.get((h.getYPos() - g.getYMax())*-1).get(h.getXPos()).setImage("hunter.png");
					map.get((h.getYPos() - g.getYMax())*-1).get(h.getXPos()).setName(h.getName() + " has " + h.getKills() + " kills.");
				}
				
				m_Request.setAttribute("game", g);
				m_Request.setAttribute("map", map);
				redirect("status.jsp");	
				
					
				
				
			} catch (Exception e) {
				new LogWriter().write(e);
				e.printStackTrace();
			}
		}
		
		
	}
	
	
	public class FieldBean  {
		private String m_Name = "";
		private String m_Image = "empty.png";
		
		public String getName() {
			return m_Name;
		}
		
		public void setName(String name) {
			m_Name = name;
		}
		
		public String getImage() {
			return m_Image;
		}
		
		public void setImage(String image) {
			m_Image = image;
		}		
	}

}


// end of Info.java
